// pages/production/production.js
const app = getApp();
Page({

    data: {
        loading: true,//上拉变量
        loadingconflg: true, //没有数据时变量,
        page: 1,//页数
        listdata: [],//数据
        Count: "",//总页数
        iscompile: true,//是否显示编辑
        nodata: false,//无数据
        totalpage: 1//总页数
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: '作品管理',
        });
        var _this = this;
        _this.getMyList(_this.data.page);
    },
    //点击删除
    clickdelimg: function (e) {

        var _this = this;
        var listdata = _this.data.listdata;
        var arr = [];
        listdata.forEach((obj, i) => {
            if (obj.ishow == true) {
                arr.push(obj.Id)
            }
        });
        app.http.POST("/MiniCardOpus/DeleteOpus", { "ids": arr }, (err, data) => {
            console.log(data)
            if (data.code == 1) {
                wx.showToast({
                    title: '删除成功',
                    icon: 'success',
                    duration: 2000,
                    success: function (res) {
                        _this.setData({
                            "iscompile": true,
                            "listdata": []
                        })
                        _this.getMyList(1);
                    }
                })
            } else {
                wx.showToast({
                    title: '删除失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
        console.log(arr);
    },
    //点击图片
    clickimgs: function (e) {
        console.log(e);
        var item = e.currentTarget.dataset.item;
        var isdel = this.data.iscompile;
        var index = e.currentTarget.dataset.index;
        var data = this.data.listdata;
        var arr = [];
        console.log(data);
        if (isdel) {
            data.forEach(function (obj, i) {
                arr.push(obj.AttUrl)
            })
            wx.previewImage({
                current: arr[index],
                urls: arr
            })
        } else {
            //点击图片删除标记
            if (item.ishow) {
                item.ishow = false;
            } else {
                item.ishow = true;
            }
            var obj = {};
            obj['listdata[' + index + ']'] = item;
            this.setData(obj);

        }
    },
    //点击编辑
    clickcomp: function (e) {
        this.setData({
            "iscompile": false
        })
    },
    //发布
    insertOpus: function (e) {
        wx.navigateTo({
            url: '../imgupload/imgupload?photoid=3'
        })
    },
    //获取作品信息
    getMyList: function (page) {
        var _this = this;
        var listdata = _this.data.listdata;
        wx.showLoading({
            title: '加载中',
        })
        app.http.POST("/MiniCardOpus/GetMyOpusList", { "page": page }, (err, data) => {
            if (data.code == 1) {
                if (data.data.List == null || data.data.List.length < 1) {
                    this.setData({
                        "loading": false,
                        "loadingconflg": false,
                        "nodata": true
                    })
                } else {
                    listdata = listdata.concat(data.data.List);
                    _this.setData({
                        "listdata": listdata,
                        "totalpage": data.data.PageCount,
                        "loading": false,
                        "loadingconflg": false,
                        "nodata": false
                    })
                }
            } else {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none',
                    duration: 2000
                })
                _this.setData({
                    "loading": false,
                    "nodata": true
                })
            }
            wx.hideLoading();
        })
    },
    //滚动加载
    lower: function (e) {
        var _this = this;
        var page = _this.data.page + 1;
        if (page <= _this.data.totalpage) {
            _this.setData({
                "page": page,
                "loading": true,
                "loadingconflg": true,
            })
            _this.getMyList(page);
        } else {
            _this.setData({
                "loading": true,
                "loadingconflg": false,
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    reload:function(){
      var _this = this;
      _this.setData({
        listdata: []
      })
      _this.getMyList(1);
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var userInfo = app.globalData.userInfo;
        //判断当前用户是否开通服务,开通则分享美名片页,否则分享访问记录页
        if (userInfo == null || typeof (userInfo) == "undefined") {
            app.getUserall(function (res) {
                userInfo = res;
            });
        }
        var path = userInfo.UserType == 2 ? "/pages/index/index?cid=" + userInfo.Id : "/pages/cardrecord/cardrecord";
        return {
            title: userInfo.NickName + "的美名片",
            path: path,
            success: function (res) {
                wx.showToast({
                    title: '分享成功',
                    icon: "success",
                    duration: 200
                })
            }
        }
    }
})